"use strict"
var require = function(src) { Require._load(src) }

var Require = {
	_loaded: {},

	baseURL: "scripts/",
	
	_load: function(fname) {
		var that = this
		if (typeof fname !== "string") {
			throw "file name not a string"
		} else if (!fname.match(/\.js$/)) {
			throw "not a .js file"
		} else if (that._loaded[fname]) {
			return true
		}
		
		that._loaded[fname] = false
		
		var scr = document.createElement("script")
		scr.src = that.baseURL + fname + "?time=" + new Date().getTime()
		scr.type = "text/javascript"
		scr.onload = function() {
			that._loaded[fname] = true
			that._checkLoaded()
		}
		scr.onerror = function() {
			delete this
			throw "require failed on file: " + fname
		}
		document.head.appendChild(scr)
	},
	
	onLoad: null,
	
	_checkLoaded: function() {
		for (var i in this._loaded) {
			if (this._loaded[i] !== true) { return }
		}
		if (this.onLoad) { this.onLoad() }
	}
};